package cn.iocoder.yudao.module.call.controller.admin.ivrflow;

import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrintentionlabelnotmatch.IvrIntentionLabelNotMatchDO;
import cn.iocoder.yudao.module.call.dal.mysql.ivrintentionlabelnotmatch.IvrIntentionLabelNotMatchMapper;
import cn.iocoder.yudao.module.system.api.extend.ExtendApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.ivrflow.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrflow.IvrFlowDO;
import cn.iocoder.yudao.module.call.service.ivrflow.IvrFlowService;

@Tag(name = "管理后台 - ivr流程")
@RestController
@RequestMapping("/call/ivr-flow")
@Validated
public class IvrFlowController {

    @Resource
    private IvrFlowService ivrFlowService;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private ExtendApi extendApi;

    @PostMapping("/training")
    @Operation(summary = "创建ivr流程")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<JSONObject> ivrFlowTraining( @RequestBody TrainingReqVO trainingReqVO) {
        JSONObject training = ivrFlowService.training(trainingReqVO.getTrainingId(), trainingReqVO.getMessage(), trainingReqVO.getFlowId());
        return success(training);
    }

    @PostMapping("/create")
    @Operation(summary = "创建ivr流程")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createIvrFlow(@Valid @RequestBody IvrFlowSaveReqVO createReqVO) {
        return success(ivrFlowService.createIvrFlow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ivr流程")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:update')")
    public CommonResult<Boolean> updateIvrFlow(@Valid @RequestBody IvrFlowSaveReqVO updateReqVO) {
        redisTemplate.delete("ivrFlow:" + updateReqVO.getId());
        ivrFlowService.updateIvrFlow(updateReqVO);
        return success(true);
    }

    @GetMapping("/updateStatus")
    @Operation(summary = "更新ivr流程状态")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:update')")
    public CommonResult<Boolean> updateStatus(Long id) {
        ivrFlowService.updateStatus(id);
        redisTemplate.delete("ivrFlow:" + id);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ivr流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:delete')")
    public CommonResult<Boolean> deleteIvrFlow(@RequestParam("id") Long id) {
        redisTemplate.delete("ivrFlow:" + id);
        ivrFlowService.deleteIvrFlow(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ivr流程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<IvrFlowRespVO> getIvrFlow(@RequestParam("id") Long id) {
        IvrFlowDO ivrFlow = ivrFlowService.getIvrFlow(id);
        return success(BeanUtils.toBean(ivrFlow, IvrFlowRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ivr流程分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<PageResult<IvrFlowRespVO>> getIvrFlowPage(@Valid IvrFlowPageReqVO pageReqVO) {
        PageResult<IvrFlowDO> pageResult = ivrFlowService.getIvrFlowPage(pageReqVO);
        pageResult.getList().forEach(item -> {
            AdminUserRespDTO userCreator = adminUserApi.getUser(Long.valueOf(item.getCreator()));
            AdminUserRespDTO userUpdater = adminUserApi.getUser(Long.valueOf(item.getUpdater()));
            item.setCreatorName(userCreator.getNickname());
            item.setUpdaterName(userUpdater.getNickname());
        });
        return success(BeanUtils.toBean(pageResult, IvrFlowRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得ivr流程列表")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<List<IvrFlowRespVO>> getIvrFlowList(@Valid IvrFlowPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<IvrFlowDO> pageResult = ivrFlowService.getIvrFlowPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult.getList(), IvrFlowRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ivr流程 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrFlowExcel(@Valid IvrFlowPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrFlowDO> list = ivrFlowService.getIvrFlowPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ivr流程.xls", "数据", IvrFlowRespVO.class,
                BeanUtils.toBean(list, IvrFlowRespVO.class));
    }

    @GetMapping("/pinyin")
    @Operation(summary = "文字转拼音")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<String> pinyin(@RequestParam("keys") String keys) {
        return success(extendApi.pinyin(keys));
    }
}